import 'package:fake_handwriting/Constant.dart';
import 'package:fake_handwriting/Util.dart';
import 'package:fake_handwriting/generated/l10n.dart';
import 'package:fake_handwriting/http/http.dart';
import 'package:flutter/material.dart';
class Mine extends StatefulWidget {
  const Mine({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => MinePage();
}
class FuncBean {
  IconData icon;
  String title;
  FuncBean({required this.title, required this.icon});
}
class MinePage extends State<Mine> {
  List<FuncBean> func =[];
  bool isLogin = false;
  String nickname = "";
  String email = "";
  @override
  void initState() {
    super.initState();
    init();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Constant.paper,
      body: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.all(15),
              child: InkWell(
                onTap: () {
                  if (isLogin) {
                    Navigator.pushNamed(context, "account").then((value) {
                      if (value.runtimeType == String) {
                        nickname = value as String;
                      }
                      init();
                    });
                  }
                },
                child: Container(
                  decoration: BoxDecoration(
                      color: Colors.white, borderRadius: Constant.borderRadius),
                  width: double.infinity,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(vertical: 30),
                    child: isLogin
                        ? Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 30),
                      child: Column(
                        children: [
                          Text(
                            nickname,
                            style: const TextStyle(fontSize: 20),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          Padding(
                              padding: const EdgeInsets.only(top: 10),
                              child: Text(
                                email,
                                style: const TextStyle(
                                  overflow: TextOverflow.ellipsis,
                                ),
                              )),
                        ],
                      ),
                    )
                        : Column(
                      children: [
                        InkWell(
                          onTap: () {
                            Navigator.pushNamed(context, "login").then((value) {
                              init();
                            });
                          },
                          child: Container(
                            padding: const EdgeInsets.symmetric(
                                vertical: 15, horizontal: 60),
                            decoration: const BoxDecoration(
                                color: Colors.blue,
                                borderRadius: BorderRadius.horizontal(
                                    left: Radius.circular(30),
                                    right: Radius.circular(30))),
                            child:Text(
                              S.of(context).clickToLogin,
                              style: const TextStyle(color: Colors.white, fontSize: 20),
                            ),
                          ),
                        ), Padding(
                            padding: const EdgeInsets.only(top: 15),
                            child: Text(S.of(context).startCreationAfterLogin))
                      ],
                      mainAxisAlignment: MainAxisAlignment.center,
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Padding(
                padding:
                const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                child: GridView.builder(
                    itemCount: func.length,
                    gridDelegate:
                    const SliverGridDelegateWithFixedCrossAxisCount(
                      //横轴元素个数
                        crossAxisCount: 3,
                        //纵轴间距
                        mainAxisSpacing: 5,
                        //横轴间距
                        crossAxisSpacing: 5),
                    itemBuilder: (BuildContext context, int index) {
                      return InkWell(
                        onTap: () async {
                          if (isLogin) {
                            Util.println(func[index].title.toString());
                            var title=func[index].title.toString();

                            if(title==S.of(context).changePassword){
                              Navigator.pushNamed(context, "reset_password").then((value) {
                                if (value.runtimeType == bool) {
                                  var v = value as bool;
                                  if (v) {
                                    isLogin = false;
                                    setState(() {});
                                    Navigator.pushNamed(context, "login").then((value) {
                                      if (value.runtimeType == bool) {
                                        isLogin = value as bool;
                                      }
                                      init();
                                    });
                                  }
                                }
                                init();
                              });
                            }

                            if(title==S.of(context).myCreation){
                              Navigator.pushNamed(context, "master");
                            }
                            if(title==S.of(context).myFont){
                              Navigator.pushNamed(context, "online_font", arguments: "manage");
                            }
                            if(title==S.of(context).myBackground){
                              Navigator.pushNamed(context, "online_image", arguments: "manage");
                            } if(title==S.of(context).privacyPolicy){
                              Navigator.pushNamed(context, "web", arguments: "${Http.BASE_URL}policy/?type=0");
                            }
                            if(title==S.of(context).serviceItems){
                              Navigator.pushNamed(context, "web", arguments: "${Http.BASE_URL}policy/?type=1");
                            }  if(title==S.of(context).logout){
                              await Util.removeToken();
                              isLogin = false;
                              setState(() {});
                            }
                          } else {
                            Util.toast(S.of(context).pleaseLoginAtFirst);
                          }
                        },
                        child: Column(
                          children: [
                            Icon(
                              func[index].icon,
                              color: isLogin ? Colors.black : Colors.grey,
                            ),
                            Padding(
                                padding: const EdgeInsets.only(top: 10),
                                child: Text(
                                  func[index].title,
                                  style: TextStyle(
                                      color:
                                      isLogin ? Colors.black : Colors.grey),
                                )),
                          ],
                          mainAxisAlignment: MainAxisAlignment.center,
                        ),
                      );
                    }),
              ),
            ),
          ],
        ),
      ),
      appBar: AppBar(
        title:Text(S.of(context).profile),
      ),
    );
  }
  Future<void> init() async {
    Future.delayed(Duration.zero).then((value) async {
      func.clear();
      func.addAll([
        FuncBean(title: S.of(context).changePassword, icon: Icons.password),
        FuncBean(title: S.of(context).myCreation, icon: Icons.apps_rounded),
        FuncBean(title: S.of(context).myFont, icon: Icons.font_download),
        FuncBean(title: S.of(context).myBackground, icon: Icons.check_box_outline_blank),
        FuncBean(title: S.of(context).privacyPolicy, icon: Icons.policy_outlined),
        FuncBean(title: S.of(context).serviceItems, icon: Icons.article_outlined),
        FuncBean(title: S.of(context).logout, icon: Icons.exit_to_app),
      ]);
      if(!await Http.validateToken()){
        await Util.removeToken();
      }else{
        var map = await Util.getTokenInfo();
        if(map==null){
          isLogin=false;
        }else{
          isLogin=true;
          nickname = map["nickname"];
          email = map["email"];
        }
      }
      setState(() {});
    });
  }
}